package com.atguigu.eduService.service.impl;

import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.entity.vo.TeacherQuery;
import com.atguigu.eduService.mapper.EduTeacherMapper;
import com.atguigu.eduService.service.EduTeacherService;
import com.atguigu.eduService.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author smday
 * @since 2020-07-25
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {


    @Override
    public IPage<EduTeacher> pageQuery(Integer current, Integer size, TeacherQuery teacherQuery) {

        Page<EduTeacher> page = new Page<>(current, size);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(teacherQuery.getName())){
            wrapper.like("name",teacherQuery.getName());
        }
        if(!StringUtils.isEmpty(teacherQuery.getLevel())){
            wrapper.eq("level",teacherQuery.getLevel());
        }
        if(!StringUtils.isEmpty(teacherQuery.getBegin())){
            wrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if(!StringUtils.isEmpty(teacherQuery.getEnd())){
            wrapper.le("gmt_create",teacherQuery.getEnd());
        }
        wrapper.orderByDesc("gmt_create");
        return baseMapper.selectPage(page, wrapper);


    }

    @Cacheable(value = "guli:cms:teacher",key = "'teacher'")
    @Override
    public List<EduTeacher> listTeacher() {
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        return baseMapper.selectList(wrapperTeacher);
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> page) {

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("gmt_create");
        baseMapper.selectPage(page,wrapper);
        return PageUtils.convertPageToMap(page);
    }
}
